package ewa.big.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

/**
 * <b>Web Engineering - UE3</b>
 * <p>
 *
 * This class represents the registered player pool of the "Mensch aergere Dich nicht" game.
 * 
 * @version 1.00 (01.05.2012)
 *  
 */
@ManagedBean(name = "rpp")
@ApplicationScoped
public class RegisteredPlayerPool implements Serializable {
    private static final long serialVersionUID = 1L;

    HashMap<String, Player> regplayers = null;

    /** Creates a new instance of RegisteredPlayerPool */
    public RegisteredPlayerPool() {
        regplayers = new HashMap<String, Player>();

        //Add test player
        Player tp = new Player();
        tp.setUsername("supermario");
        tp.setPassword("supermario");
        regplayers.put("supermario", tp);
    }

    /**
     * Add a player to the registered player pool.
     * 
     * @param p    the player to add.
     * @return  "true" if the player added successfully, "false" otherwise.
     */
    public boolean addPlayer(Player p) {
        if (!regplayers.containsKey(p.getUsername())) {
            regplayers.put(p.getUsername(), p);
            return true;
        } else {
            return false;
        }
    } // end addPlayer

    /**
     * Get the registered player.
     * 
     * @param username    the username.
     * @param password    the password.
     * @return the player or "null" if the player not registered.
     */
    public Player getRegisteredPlayer(String username, String password) {
        Player curplayer;
        if ((curplayer = regplayers.get(username)) != null) {
            if (curplayer.getPassword().equals(password)) {
                return curplayer;
            }
        }
        return null;
    } // end getRegisteredPlayer

    /**
     * Get the list of registered players.
     * 
     * @return the players
     */
    public List<Player> getRegplayers() {
        return new ArrayList<Player>(regplayers.values());
    } // end getRegplayers
    
} // end of class RegisteredPlayerPool
